/*
 * CreditDetailPanel.java
 *
 * Created on 2008年5月27日, 下午10:09
 */

package gui;
import javax.swing.table.DefaultTableModel;
import model.CreditCardAccount;
import model.CreditCardAccountInfo;
import model.CreditCardDetailInfo;
import java.util.Vector;
import abstractmodel.IDetail;
import java.util.ArrayList;
import model.Detail;
import abstractmodel.IAccount;
import abstractmodel.IInfo;
import abstractmodel.InfoValue;

/**
 *
 * @author  yux
 */
public class CreditDetailPanel extends javax.swing.JPanel {
    private CreditCardAccount account;
    private int column=model.CreditCardDetailInfo.creditCardDetailInfoTitles.length;
    private int init = 0;
    private String[] store;
    /** Creates new form CreditDetailPanel */
    public CreditDetailPanel(IAccount a) {
        account = (CreditCardAccount)a;
        initComponents();
        javax.swing.table.DefaultTableModel md = 
                new javax.swing.table.DefaultTableModel(CreditCardDetailInfo.creditCardDetailInfoTitles, 0);
        this.jTableDetail.setModel(md);
        initTable();
        jTableDetail.setAutoCreateRowSorter(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableDetail = new javax.swing.JTable();
        jPanelSet = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldCanUse = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldDate = new javax.swing.JTextField();
        jTextFieldLeave = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldReason = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldAmount = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextFieldOther = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jTableDetail.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "收入/支出日期", "原因", "交易金额", "帐户余额", "可用额度", "备注"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableDetail.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableDetailMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableDetail);

        jPanelSet.setLayout(new java.awt.CardLayout());

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 719, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 209, Short.MAX_VALUE)
        );

        jPanelSet.add(jPanel1, "card2");

        jLabel5.setText("可用额度");

        jLabel4.setText("余额");

        jLabel1.setText("收支日期");

        jLabel2.setText("原因");

        jLabel3.setText("数额");

        jLabel7.setText("备注");

        jButton1.setText("确定");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("删除");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldDate, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldReason, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addComponent(jLabel5)
                                    .addGap(18, 18, 18)
                                    .addComponent(jTextFieldCanUse))
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addGap(62, 62, 62)
                                    .addComponent(jTextFieldLeave, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(4, 4, 4)))
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextFieldOther, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButton1))
                        .addGap(27, 27, 27)
                        .addComponent(jButton2)))
                .addGap(123, 123, 123))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldReason, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(45, 45, 45)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldLeave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jTextFieldOther, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldCanUse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        jPanelSet.add(jPanel2, "card3");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 719, Short.MAX_VALUE)
            .addComponent(jPanelSet, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelSet, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTableDetailMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableDetailMouseClicked
        // TODO add your handling code here:
        java.awt.CardLayout cl = (java.awt.CardLayout)jPanelSet.getLayout();
        if (init == 0) {
            init = 1;
            cl.next(jPanelSet);
        }
        int index = jTableDetail.getSelectedRow();
        ArrayList<IDetail> detailList = (ArrayList<IDetail>)account.getDetailsInfo();
        Detail detail = (Detail)detailList.get(index);
        jTextFieldDate.setText(detail.getDetailInfo().getValue("收支日期").getString());
        jTextFieldAmount.setText(detail.getDetailInfo().getValue("数额").getString());
        jTextFieldLeave.setText(detail.getDetailInfo().getValue("余额").getString());
        jTextFieldReason.setText(detail.getDetailInfo().getValue("原因").getString());
        jTextFieldCanUse.setText(detail.getDetailInfo().getValue("可用额度").getString());
        jTextFieldOther.setText(detail.getDetailInfo().getValue("备注").getString());
        store = new String[column];
        store[0] = jTextFieldDate.getText();
        store[1] = jTextFieldAmount.getText();
        store[2] = jTextFieldLeave.getText();
        store[3] = jTextFieldReason.getText();
        store[4] = jTextFieldCanUse.getText();
        store[5] = jTextFieldOther.getText();
    }//GEN-LAST:event_jTableDetailMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        int index = jTableDetail.getSelectedRow();
        ArrayList<IDetail> detailList = (ArrayList<IDetail>)account.getDetailsInfo();
        Detail detail = (Detail)detailList.get(index);
        int fault = 0;
        IInfo info=detail.getDetailInfo();
        if (!info.setValue("收支日期", jTextFieldDate.getText()))
            fault = 1;
        else if (!info.setValue("数额", jTextFieldAmount.getText()))
            fault = 2;
        else if (!info.setValue("余额", jTextFieldLeave.getText()))
            fault = 3;
        else if (!info.setValue("原因", jTextFieldReason.getText()))
            fault = 4;
        else if (!info.setValue("可用额度", jTextFieldCanUse.getText()))
            fault = 5;
        else if (!info.setValue("备注", jTextFieldOther.getText()))
            fault = 6;
        //setback
        if (fault >= 1)
            jTextFieldDate.setText(store[0]);
        if (fault >= 2)
            jTextFieldAmount.setText(store[1]);
        if (fault >= 3)
            jTextFieldLeave.setText(store[2]);
        if (fault >= 4)
            jTextFieldReason.setText(store[3]);
        if (fault >= 5)
            jTextFieldCanUse.setText(store[4]);
        if (fault == 6)
            jTextFieldOther.setText(store[5]);
        //alarm
        if (fault != 0) {
            String[] options = {"确定"};
            javax.swing.JOptionPane.showOptionDialog(null, "更新明细失败，请重新更新！", 
                    "警告", javax.swing.JOptionPane.DEFAULT_OPTION, 
                    javax.swing.JOptionPane.WARNING_MESSAGE, null, options, options[0]); 
        }
        detail.setDetailInfo(info);
        initTable();
        jTableDetail.clearSelection();
        jTableDetail.addRowSelectionInterval(index, index);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        int index = jTableDetail.getSelectedRow();
        ArrayList<IDetail> detailList = (ArrayList<IDetail>)account.getDetailsInfo();
        Detail detail = (Detail)detailList.get(index);
        this.account.deleteDetailInfo(detail);
        initTable();
        jTableDetail.clearSelection();
        //this.getParent().removeAll();
    }//GEN-LAST:event_jButton2ActionPerformed
    
    private void initTable() {      
        javax.swing.table.DefaultTableModel md=(DefaultTableModel) jTableDetail.getModel();
        md.setNumRows(0);
        ArrayList<IDetail> detailList = (ArrayList<IDetail>)account.getDetailsInfo();
        int number = detailList.size();
        System.out.println(number);
        //jTableDetail.setModel(md);
        for (int i = 0; i < number; i++) {
            Detail detail = (Detail)detailList.get(i);
            Vector show = new Vector(column);
            for (int j = 0; j < column; j++)  {
                InfoValue infov=detail.getDetailInfo().getValue(CreditCardDetailInfo.creditCardDetailInfoTitles[j]);
                Object o=null;
                if(infov.getType()==Double.class) {
                    o=new Double(infov.getDouble());
                }else {
                    o=infov.getString();
                }
                show.add(o);
            }
            md.addRow(show);
        }
        //jTableDetail.getModel().
        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelSet;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableDetail;
    private javax.swing.JTextField jTextFieldAmount;
    private javax.swing.JTextField jTextFieldCanUse;
    private javax.swing.JTextField jTextFieldDate;
    private javax.swing.JTextField jTextFieldLeave;
    private javax.swing.JTextField jTextFieldOther;
    private javax.swing.JTextField jTextFieldReason;
    // End of variables declaration//GEN-END:variables
    
}
